//
//  LoanSelectView.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/12/6.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

@objc protocol LoanSelectViewDelegate {
    @objc optional func selectLoanType(currentView:LoanSelectView) //选择贷款类型
    @objc optional func getInputText(currentView:LoanSelectView) //获取文本内容
}

class LoanSelectView: UIView {
    
    weak var delegate : LoanSelectViewDelegate?

    fileprivate let titleLabel = UILabel() //标题
    fileprivate let textView = UIView()
    let textField = UITextField.init(Width: 5) //内容
    fileprivate let manButton = UIButton() //是(男)
    fileprivate let manLabel = UILabel()
    fileprivate let womanButton = UIButton() //否(女)
    fileprivate let womanLabel = UILabel()
    
    var isYes = "" //选中目标,1为第一个,2为第二个
    
    //是否显示选择按钮
    var showSelect : Bool = true {
        didSet{
            if !showSelect {
                manButton.isHidden = true
                manLabel.isHidden = true
                womanLabel.isHidden = true
                womanButton.isHidden = true
                
                textField.snp.remakeConstraints { (make) in
                    make.edges.equalToSuperview().inset(UIEdgeInsets.zero)
                }
            }
        }
    }
    
    var placeholder : String! {
        didSet{
            textField.placeholder = placeholder
        }
    }
    
    //是否显示输入框
    var isHide : Bool = true {
        didSet{
            if !isHide{
                textField.isHidden = false
            }
        }
    }
    
    var title : String! {//标题
        didSet{
            titleLabel.text = title
        }
    }
    
    var text : String! {//内容
        didSet{
            textField.text = text
        }
    }
    
    init(firstText:String?=yes_title,lastText:String?=no_title,textWidth:CGFloat?=ScreenW/3*2) {
        super.init(frame: .zero)
        
        titleLabel.numberOfLines = 0
        titleLabel.textAlignment = .right
        titleLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 16))
        self.addSubview(titleLabel)
        
        textView.layer.borderColor = UIColor.darkGray.cgColor
        textView.layer.borderWidth = 1
        self.addSubview(textView)
        
        textField.isHidden = true
        textField.font = UIFont.systemFont(ofSize: RATIO(maxNum: 16))
        textField.addTarget(self, action: #selector(textFieldDidChange), for: .editingChanged)
        textView.addSubview(textField)
        
        manButton.layer.cornerRadius = 10
        manButton.layer.masksToBounds = true
        manButton.layer.borderColor = UIColor.darkGray.cgColor
        manButton.layer.borderWidth = 1
        manButton.tag = 1
        manButton.addTarget(self, action: #selector(selectType(sender:)), for: .touchUpInside)
        textView.addSubview(manButton)
        
        manLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 16))
        manLabel.text = firstText
        textView.addSubview(manLabel)
        
        womanButton.layer.cornerRadius = 10
        womanButton.layer.masksToBounds = true
        womanButton.layer.borderWidth = 1
        womanButton.layer.borderColor = UIColor.darkGray.cgColor
        womanButton.tag = 2
        womanButton.addTarget(self, action: #selector(selectType(sender:)), for: .touchUpInside)
        textView.addSubview(womanButton)
        
        womanLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 16))
        womanLabel.text = lastText
        textView.addSubview(womanLabel)
        
        textView.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(RATIO(maxNum: -20))
            make.centerY.equalToSuperview()
            make.width.equalTo(textWidth!)
            make.height.equalTo(35)
        }
        
        titleLabel.snp.makeConstraints { (make) in
            make.right.equalTo(textView.snp.left).offset(RATIO(maxNum: -5))
            make.left.equalToSuperview().offset(RATIO(maxNum: 10))
            make.top.equalToSuperview()
            make.bottom.equalToSuperview()
            
        }
        
        womanLabel.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(RATIO(maxNum: -5))
            make.centerY.equalToSuperview()
            make.width.equalTo((womanLabel.text?.size(font: RATIO(maxNum: 16), height: 20).width)!-10)
            make.height.greaterThanOrEqualTo(10)
        }
        
        womanButton.snp.makeConstraints { (make) in
            make.right.equalTo(womanLabel.snp.left).offset(RATIO(maxNum: -2))
            make.centerY.equalTo(womanLabel.snp.centerY)
            make.width.height.equalTo(20)
        }
        
        manLabel.snp.makeConstraints { (make) in
            make.right.equalTo(womanButton.snp.left).offset(RATIO(maxNum: -5))
            make.centerY.equalTo(womanButton)
            make.width.equalTo((manLabel.text?.size(font: RATIO(maxNum: 16), height: 20).width)!-10)
            make.height.greaterThanOrEqualTo(10)
        }
        
        manButton.snp.makeConstraints { (make) in
            make.right.equalTo(manLabel.snp.left).offset(RATIO(maxNum: -2))
            make.centerY.equalTo(manLabel)
            make.width.height.equalTo(20)
        }
        
        textField.snp.makeConstraints { (make) in
            make.left.centerY.height.equalToSuperview()
            make.right.equalTo(manButton.snp.left)
        }

    }
    
    //MARK: 选择类型
    @objc fileprivate func selectType(sender:UIButton) {
        
        if sender.tag == 1 {
            manButton.backgroundColor = UIColor.darkGray
            womanButton.backgroundColor = UIColor.clear
        }else{
            manButton.backgroundColor = UIColor.clear
            womanButton.backgroundColor = UIColor.darkGray
        }
        
        self.isYes = "\(sender.tag)"
        
        if self.delegate != nil {
            self.delegate?.selectLoanType!(currentView: self)
        }
    }
    
    //MARK: 监听文输入本框内容
    @objc fileprivate func textFieldDidChange() {
        
        text = textField.text
        self.delegate?.getInputText!(currentView: self)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
}

